package com.nwpu.server.learning.controller;

import com.alibaba.fastjson.JSONObject;
import com.nwpu.server.common.response.PageUtils;
import com.nwpu.server.common.response.R;
import com.nwpu.server.common.util.FormatUtils;
import com.nwpu.server.common.util.SecurityUtils;
import com.nwpu.server.common.util.TokenUtils;
import com.nwpu.server.learning.annotation.OperationLogAspect;
import com.nwpu.server.learning.entity.TaskEntity;
import com.nwpu.server.learning.enums.StatusEnum;
import com.nwpu.server.learning.service.FileService;
import com.nwpu.server.learning.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;


/**
 * 任务
 *
 * @author YuXingZh
 * @email 617058979@qq.com
 * @date 2022-01-23 16:24:31
 */
@Slf4j
@RestController
@RequestMapping("learning/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = taskService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        TaskEntity task = taskService.getById(id);

        return R.ok().put("task", task);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TaskEntity task) {
        taskService.save(task);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TaskEntity task) {
        taskService.updateById(task);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        taskService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 修改
     */
    @OperationLogAspect(module = "用户模块", description = "添加备注", value = "添加备注")
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody TaskEntity task) {
        taskService.updateById(task);

        return R.ok();
    }

//    @OperationLogAspect(module = "用户模块", description = "新方法运算", value = "运算")
//    @ResponseBody
//    @RequestMapping(value = "/run", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    public R run(@RequestBody JSONObject jsonObject) {
//        log.info("写入任务");
//
//        // 任务id
//        String taskId = FormatUtils.getUUID();
//
//        Date date = new Date();
//        // 写入任务
//        TaskEntity taskEntity = new TaskEntity();
//        taskEntity.setCreateTime(date);
//        taskEntity.setId(taskId);
//        taskEntity.setStatus(StatusEnum.NEW.getValue());
//        taskEntity.setTaskSn(taskId);
//        taskEntity.setUserId(SecurityUtils.getUserId());
//        taskEntity.setUsername("yuxingzh");
//        taskService.save(taskEntity);
//        taskService.run(jsonObject);
//        return R.ok();
//    }
}
